﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_consultas : Form
    {
        System.Data.SqlClient.SqlConnection con;

        public Animal an3;
        private string op;
        public string nc;
        private int ide;
        
        public frm_consultas(Animal an2, string nomec, string op1, int ide)
        {
            this.an3 = an2;
            this.nc = nomec;
            this.op = op1;
            this.ide = ide;

            InitializeComponent();

            if (op == "animal")
            {
                txtnomecli.Text = nc;
                txtnomeani.Text = an3.get_nome().ToString();
                txtnchip.Text = an3.get_chip().ToString();
                txtdtnasc.Text = an3.get_dtnasc().ToString();
            }
            else
            {
                if (op == "exploracao")
                {
                    txtnomecli.Text = nc;
                    txtdtnasc.Visible = false;
                    lbldtnasc.Visible = false;
                    txtnchip.Visible = false;
                    lblnchip.Visible = false;
                    lblnomeanimal.Visible = false;
                    txtnomeani.Visible = false;

                    cmdinscons.Visible = false;
                }
            }

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            pesquisar_consultas();
        }

        private void pesquisar_consultas()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            if (op == "animal")
            {

                int ida = an3.get_id();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_consulta, data_consulta, hora_consulta, tipo_consulta, anamenese, diagnostico, tratamento, pre_peso, pre_temperatura, id_animal FROM Consulta WHERE id_animal = " + ida + " and tipo_consulta = 'Animal'";
                SqlDataReader readatrib = command1.ExecuteReader();

                this.dataGridView1.Rows.Clear();

                while (readatrib.Read())
                {
                    int i = this.dataGridView1.Rows.Add();

                    this.dataGridView1[0, i].Value = readatrib["id_consulta"].ToString();
                    this.dataGridView1[1, i].Value = readatrib["data_consulta"].ToString();
                    this.dataGridView1[2, i].Value = readatrib["hora_consulta"].ToString();
                    this.dataGridView1[3, i].Value = readatrib["tipo_consulta"].ToString();
                    this.dataGridView1[4, i].Value = readatrib["anamenese"].ToString();
                    this.dataGridView1[5, i].Value = readatrib["diagnostico"].ToString();
                    this.dataGridView1[6, i].Value = readatrib["tratamento"].ToString();
                    this.dataGridView1[7, i].Value = readatrib["pre_peso"].ToString();
                    this.dataGridView1[8, i].Value = readatrib["pre_temperatura"].ToString();
                    this.dataGridView1[9, i].Value = readatrib["id_animal"].ToString();

                }

                con.Close();
            }
            else
            {
                if (op == "exploracao")
                {
                    SqlCommand command1 = con.CreateCommand();
                    command1.CommandText = "SELECT id_consulta, data_consulta, hora_consulta, tipo_consulta, anamenese, diagnostico, tratamento, pre_peso, pre_temperatura, id_animal FROM Consulta WHERE id_animal = " + ide + " and tipo_consulta = 'Animal exploracao'";
                    SqlDataReader readatrib = command1.ExecuteReader();

                    this.dataGridView1.Rows.Clear();

                    while (readatrib.Read())
                    {
                        int i = this.dataGridView1.Rows.Add();

                        this.dataGridView1[0, i].Value = readatrib["id_consulta"].ToString();
                        this.dataGridView1[1, i].Value = readatrib["data_consulta"].ToString();
                        this.dataGridView1[2, i].Value = readatrib["hora_consulta"].ToString();
                        this.dataGridView1[3, i].Value = readatrib["tipo_consulta"].ToString();
                        this.dataGridView1[4, i].Value = readatrib["anamenese"].ToString();
                        this.dataGridView1[5, i].Value = readatrib["diagnostico"].ToString();
                        this.dataGridView1[6, i].Value = readatrib["tratamento"].ToString();
                        this.dataGridView1[7, i].Value = readatrib["pre_peso"].ToString();
                        this.dataGridView1[8, i].Value = readatrib["pre_temperatura"].ToString();
                        this.dataGridView1[9, i].Value = readatrib["id_animal"].ToString();

                    }

                    con.Close();
                }
            }
        }

        private void frm_consultas_Load(object sender, EventArgs e)
        {

        }

        private void cmdinscons_Click(object sender, EventArgs e)
        {
            if (op == "animal")
            {
                frm_novaconsulta frm1 = new frm_novaconsulta(an3, nc);
                this.Hide();
                frm1.ShowDialog();
            }
        }

        private void cmdvoltar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
